package cn.psvmc.cxzapi.controller.store;

import cn.psvmc.cxzapi.model.common.SearchEntity;
import cn.psvmc.cxzapi.model.store.*;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.store.TAccountService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 账户管理
 * @Author auto
 * @Date 2024-08-14
 */
@RestController
@RequestMapping("/account")
public class TAccountController {

    @Resource
    private TAccountService tAccountService;

    /**
     * @Description 充值
     * @Date 2024/8/16 8:45
     * @Author YWX
     * @Param [bill]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_bill")
    public Result addBill(@RequestBody TAccountBill bill) {
        Integer type = bill.getType();
        ToolUtils.validation(new Object[]{
                bill.getAccountId(), "账户id", 1
                , bill.getAmount(), "交易金额", 2
                , bill.getTransactionId(), "流水号", 2
                , type, "交易类型", 1
        });
        if (type.equals(3) && ToolUtils.isEmpty(bill.getOrderCode())) return ResultGenerator.genFailResult("订单号不能为空！");
        return tAccountService.addBill(bill);
    }

    /**
     * @Description 收支账单列表
     * @Date 2024/8/21 21:38
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.PageResult2
     **/
    @PostMapping("/list_bill")
    public PageResult2 listBill(@RequestBody SearchEntity entity) {
        Page<TAccount> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tAccountService.listBill(queryPage, entity));
    }

    /**
     * @Description 收支账单导出列表
     * @Date 2024/8/21 21:42
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/export_bill")
    public Result exportBill(@RequestBody SearchEntity entity) {
        return ResultGenerator.genSuccessResult(tAccountService.exportBill(entity));
    }

    /**
     * @Description 账户概览
     * @Date 2024/8/21 22:42
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail")
    public Result detail(@RequestBody SearchEntity entity) {
        Integer storeid = entity.getStoreid();
        ToolUtils.validation(new Object[]{storeid, "商家id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", tAccountService.detail(storeid));
    }

    /**
     * @Description 收支概况
     * @Date 2024/8/21 22:46
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/overview")
    public Result overview(@RequestBody SearchEntity entity) {
        Integer storeid = entity.getStoreid();
        Integer type = entity.getType();
        ToolUtils.validation(new Object[]{storeid, "商家id", 1, type, "类型", 1});
        String date;
        if (type.equals(1)) {//今天
            date = ToolUtils.getNowDateFormat("yyyyMMdd");
        } else {//昨天
            date = ToolUtils.getYesterdayDateFormat("yyyyMMdd");
        }
        return ResultGenerator.genSuccessResult("查询成功！", tAccountService.overview(storeid, date));
    }

    /**
     * @Description 提现记录
     * @Date 2024/8/15 22:16
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_record")
    public PageResult2 listRecord(@RequestBody SearchEntity entity) {
        Page<TAccount> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tAccountService.listRecord(queryPage, entity));
    }

    /**
     * @Description 记录处理
     * @Date 2024/8/15 22:36
     * @Author YWX
     * @Param [record]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/handle_record")
    public Result handleRecord(@RequestBody TAccountRecord record) {
        ToolUtils.validation(new Object[]{record.getArid(), "记录id", 1});
        tAccountService.handleRecord(record);
        return ResultGenerator.genSuccessResult("处理成功！");
    }

    /**
     * @Description 添加充值/提现申请
     * @Date 2024/8/15 22:55
     * @Author YWX
     * @Param [record]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_record")
    public Result addRecord(@RequestBody TAccountRecord record) {
        Integer type = record.getType();
        ToolUtils.validation(new Object[]{
                record.getAccountType(), "账户类型", 1
                , type, "类型", 1
                , record.getAmount(), "交易金额", 2
        });
        return ResultGenerator.genSuccessResult("申请成功！", tAccountService.addRecord(record));
    }

    /**
     * @Description 提现申请信息预览
     * @Date 2024/8/22 21:03
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/pre_record")
    public Result preRecord(@RequestBody SearchEntity entity) {
        ToolUtils.validation(new Object[]{
                entity.getAccountType(), "账户类型", 1
        });
        return ResultGenerator.genSuccessResult("获取成功！",tAccountService.preRecord(entity));
    }
}
